<template>
  <div class="dashboard-container">
    <el-card 
      class="box-card" 
      v-loading="loading"
    >
      <!-- 表单输入框 -->
      <el-form 
        ref="form" 
        :model="form" 
        label-width="80px" 
        :inline="true"
      >
        <el-form-item label="标签名称">
          <el-input v-model="form.tagName"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select 
            v-model="form.state" 
            placeholder="请选择"
          >
            <el-option 
              label="启用" 
              value="1"
            >
            </el-option>
            <el-option 
              label="禁用" 
              value="0"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 搜索/清除按钮 -->
        <el-button
          class="remove-btn"
          @click="onRemoveAll"
        >
          清除
        </el-button>
        <el-button
          type="primary"
          @click="onSearchTagsList"
        >
          搜索
        </el-button>
        <el-button
          type="primary"
          icon="el-icon-edit"
          class="addUser"
          @click="onAddtagsList"
        >
          新增标签
        </el-button>
         <el-button
          type="text"
          icon="el-icon-back"
          v-show="showBtn"
          class="showBtn"
          @click="goBack"
        >
          返回学科
        </el-button>
      </el-form>

      <!-- 新增/修改 -->
      <el-dialog
        :title="tagsForm.subjectID ? '修改标签' : '新增标签'"
        :visible.sync="addVisible"
        width="300"
        v-loading="loading"
      >
        <el-form 
          ref="tagsForm" 
          :model="tagsForm" 
          label-width="80px" 
          :rules="rules"
        >
          <el-form-item 
            label="所属学科" 
            prop="subjectID"
          >
            <el-select 
              v-model="tagsForm.subjectID" 
              placeholder="请选择活动区域"
            >
              <el-option 
                :label="item.label" 
                :value="item.value" 
                v-for="item in simpleList.data" 
                :key="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item 
            label="目录名称" 
            prop="tagName"
          >
            <el-input v-model="tagsForm.tagName"></el-input>
          </el-form-item>
        </el-form>
        <span 
          slot="footer" 
          class="dialog-footer"
        >
          <el-button @click="addVisible = false">取 消</el-button>
          <el-button 
            type="primary" 
            @click="onAddtagsListOk"
          >
            确 定
          </el-button>
        </span>
      </el-dialog>
      <!-- 提示 -->
      <el-alert
        :closable="false"
        :title="`共${tagsList.counts}条纪录`"
        type="info"
        class="hint"
        show-icon>
      </el-alert>
      <!-- 表格 -->
      <my-table
        class="container"
        :table-props="{
          data: tagsList.items,
          border: true
        }"
        :columns="[
          { label: '序号', prop: 'index', width: 180, align: 'center' },
          { label: '所属学科', prop: 'subjectName', width: 180, align: 'center'},
          { label: '标签名称', prop: 'tagName', width: 180, align: 'center' },
          { label: '创建者', prop: 'username', width: 180, align: 'center' },
          { label: '创建日期', prop: 'addDate', width: 180, align: 'center' },
          { label: '状态', prop: 'state', width: 180, width: 180, align: 'center'},
          { label: '操作', width: 180, align: 'center', slotName: 'handle', width: 300, fixed: 'right'},
        ]"

        :pagination-props="{
          layout: 'sizes, prev, pager, next, jumper',
          total: tagsList.counts,
          currentPage: page,
          pageSize: pageSize,
          pageSizes: [10, 20, 30, 40],
        }"
        :pagination-events="{
          'current-change': onCurrentPageChange,
          'size-change': onPageSizeChange
        }"
      >
      <template v-slot:handle="scope" >
        <el-button 
          type="text" 
          @click="onVisible(scope.row)"
        >
          {{scope.row.state === '已启用' ? '禁用' : '启用'}}
        </el-button>
        <el-button 
          type="text" 
          :disabled="scope.row.state === '已启用'" 
          @click="onAddtagsList(scope.row.id)"
        >
          修改
        </el-button>
        <el-button 
          type="text" 
          :disabled="scope.row.state === '已启用'" 
          @click="onRemove(scope.row.id)"
        >
          删除
        </el-button>
      </template>
      </my-table>
    </el-card>
  </div>
</template>

<script>

let currentId = null
import { mapActions, mapState } from 'vuex'
import * as tagsApi from '../../api/hmmm/tags'
export default {
  name: 'TagsList',
  data () {
    return {
      showBtn: false,
      loading: false,
      form: {
        tagName: '',
        state: ''
      },
      tagsForm: {
        subjectID: '',
        tagName: '',
      },
      page: 1,
      pageSize: 10,
      addVisible: false,
      rules: {
        subjectID: [
            { required: true, message: '请输入所属学科', trigger: 'blur' },
        ],
        tagName: [
            { required: true, message: '请输入目录名称', trigger: 'blur' }
        ],
      }
    }
  },
  computed: {
    ...mapState('tags', ['tagsList', 'simpleList', 'tagsDetail']),
  },
  methods: {
    ...mapActions('tags', ['updateTagsList', 'updateSimpleList', 'updateTagsDetail']),
    // 点击下一页
    onCurrentPageChange(val) {
      this.page = val
      for(const key in this.form) {
        if(this.form[key] !== '')  {
          console.log(this.form[key])
          console.log(345)
          this.onSearchTagsList()
        }else{
          console.log(123)
          this.updateTagsList({
          page: this.page,
          pagesize: this.pageSize
      })
        }
      }

    },
    onPageSizeChange(val) {
      this.page = 1
      this.pageSize = val
      this.updateTagsList({
      page: this.page,
      pagesize: this.pageSize
      })

    },
    // 清除按钮
    onRemoveAll() {
      this.form = {
        tagName: '',
        state: ''
      }
    },
    // 搜索按钮
    async onSearchTagsList() {
      this.loading = true
      await this.updateTagsList({
        ...this.form,
        page: this.page,
        pagesize: this.pageSize
      })
      this.loading = false
    },
    // 禁用/启用
    async onVisible(scope) {
      console.log(scope)
      await tagsApi.changeState({
        id: scope.id,
        state: scope.state === '已禁用' ? 1 : 0
      })
        .then(data => (this.$message.success('操作成功')))
        .catch(err => this.$message.error('操作失败'))
      this.updateTagsList({ page: this.page, pagesize: this.pageSize })
    },
    // 删除按钮
    onRemove(id) {
          this.$confirm('此操作将永久删除该标签, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await tagsApi.removeTags({id})
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.updateTagsList({ page: this.page, pagesize: this.pageSize })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    },
    // 点击新增/修改按钮
    async onAddtagsList(id) {
      await this.updateSimpleList()
      if(!isNaN(id)) {
        currentId = id
        await this.updateTagsDetail({id})
        this.tagsForm.subjectID = this.tagsDetail.data.subjectID
        this.tagsForm.tagName = this.tagsDetail.data.tagName
        this.addVisible = true
      }else{
        this.tagsForm.subjectID = ''
        this.tagsForm.tagName = ''
        this.addVisible = true
        currentId = null
      }
    },
    // 点击确定新增/修改
    async onAddtagsListOk() {
      this.loading = true
      if(currentId) {
        await tagsApi.updateTags({
          ...this.tagsForm,
          id: currentId
      })
        .then(res => this.$message.success('操作成功'))
        .catch(err => this.$message.error('操作失败'))
      }else{
        await tagsApi.addTags({
          ...this.tagsForm,
          id: currentId
        })
        .then(res => this.$message.success('操作成功'))
        .catch(err => this.$message.error('操作失败'))
      }
      this.loading = false
      this.addVisible = false
      this.updateTagsList({ page: this.page, pagesize: this.pageSize })
    },
    goBack() {
      this.$router.go(-1)
    }
  },
  async created() {
    this.loading = true
    if(!isNaN(this.$route.query.id)) {
      await this.updateTagsList({ page: this.page, pagesize: this.pageSize, subjectID: this.$route.query.id})
      this.showBtn = true
    }else{
      await this.updateTagsList({ page: this.page, pagesize: this.pageSize })
    }
    this.loading = false
  }
}
</script>

<style lang="scss" scoped>

.dashboard-container {
  padding: 16px;

  .remove-btn {
    margin-left: 18px;
  }

  .el-icon-film {
    color: #0000f5;
  }
  .showBtn {
    float: right;
  }
  .addUser {
    float: right;
    background-color: #7ebf50;
    border: none;
  }

  ::v-deep .ql-editor {
    height: 300px;
  }
  ::v-deep img {
    width: 100%;
  }

  .el-icon-view {
    margin: 0 5px;
  }
  .articleInfo{
    padding: 0 16px;
  }
  .hint {
    margin-bottom: 10px;
  }
  .articleContent {
    border-top: 1px dashed #ccc;
    margin-top: 5px;
    background-color: #f5f5f5;
    padding: 0 16px;
  }

}
</style>
